<?php defined('SYSPATH') or die('No direct script access.');
                          
class Culture_Controller extends Template_Controller 
{
	public $template = 'layout/culture';
	
	public function __construct()
	{
		parent::__construct();

		$this->db = new Database();

		$this->template->menu = '';
		$this->template->content = '';
		$this->template->category_name =  '';
		$this->template->leftmenu = $this->db->query('SELECT * FROM article_categories WHERE left_id>16 AND right_id <(SELECT right_id FROM article_categories WHERE id=3) AND layer_id<=3 ORDER BY left_id');
	}
	
	public function index()
	{
		$model = new Article_Model();
		$view = new View('culture/home');
		$view->overview = $model->where('name', 'culture-home-overview')->find();
		$model = new Article_Model();
		$view->whatson = $model->where('name', 'culture-home-whatson')->find();
		$model = new Article_Model();
		$view->news = $model->where('name', 'culture-home-news')->find();
		$view->categories = $this->db->query('select * from article_categories where parent_id=19;');
		
		$this->template->content =  $view->render();
	}

	public function category($name)
	{
		$model = new Article_Model();
		$view = new View('culture/'.$name);
		$view->overview = $model->where('name', 'culture-'.$name.'-overview')->find();
		$model = new Article_Model();
		$view->whatson = $model->where('name', 'culture-'.$name.'-whatson')->find();
		$model = new Article_Model();
		$view->news = $model->where('name', 'culture-home-news')->find();
		$view->categories = $this->db->query('select * from article_categories where parent_id=19;');
		
		$this->template->category_name =  $name;
		$this->template->content =  $view->render();
	}
		
	public function painting($art_type, $id)
	{
		$images = array();
		$max_height = 0;
		
		$view = new View('culture/painting');
		$realpath = realpath('images/art/'.$art_type);
		$d = dir(realpath('images/art/'.$art_type));
		while(false !== ($e = $d->read()))
		{
			if(strpos($e, '.jpg') > 0)
			{
				$images[] = 'images/art/'.$art_type.'/'.$e;
				list($width, $height, $type, $attr) = getimagesize($realpath.'/'.$e);
				
				$max_height = $max_height >= $height ? $max_height : $height;
			}
		}
		
		$view->max_height = $max_height;
		$view->images = $images;
		
		$view->category = new Article_Category_Model($id);
		
		$this->template->category_name =  'PaintingCollection';
		$this->template->content =  $view->render();
	}

}